package org.jeecg.modules.operations.assistant.crm.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.operations.assistant.crm.entity.*;
import org.jeecg.modules.operations.assistant.crm.mapper.*;
import org.jeecg.modules.operations.assistant.crm.service.ICrmWorkOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 工单信息
 * @Author: jeecg-boot
 * @Date:   2024-01-07
 * @Version: V1.0
 */
@Service
public class CrmWorkOrderServiceImpl extends ServiceImpl<CrmWorkOrderMapper, CrmWorkOrder> implements ICrmWorkOrderService {

	@Autowired
	private CrmWorkOrderMapper workOrderMapper;
	@Autowired
	private CrmInstallationOrderMapper installationOrderMapper;
	@Autowired
	private CrmMaintainOrdersMapper maintainOrdersMapper;
	@Autowired
	private CrmWorkOrderProcessMapper workOrderProcessMapper;
	@Autowired
	private CrmAttendanceRecordMapper attendanceRecordMapper;
	@Autowired
	private CrmWorkServiceRecordsMapper workServiceRecordsMapper;

	@Autowired
	private CrmFollowUpMapper followUpMapper;

	@Autowired
	private CrmWorkOrderReceiptMapper workOrderReceiptMapper;
	@Autowired
	private CrmCustomerInfoMapper customerInfoMapper;

	@Autowired
	private CrmCustomerInstallInfoMapper crmCustomerInstallInfo;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CrmWorkOrder workOrder, List<CrmInstallationOrder> installationOrderList,List<CrmMaintainOrders> maintainOrdersList,List<CrmWorkOrderProcess> workOrderProcessList) {
		workOrderMapper.insert(workOrder);
		CrmCustomerInstallInfo customerInfo = new CrmCustomerInstallInfo();
		customerInfo.setId(workOrder.getCustomerId());
		customerInfo.setCustName(workOrder.getCustomerName());
//		customerInfo.setContacts(workOrder.getContacts());
		customerInfo.setPhone(workOrder.getTelephone());
		customerInfo.setArea(workOrder.getHomeAddress());
		customerInfo.setCustAddr(workOrder.getAddress());

		if(!"".equals(workOrder.getCustomerId()) && workOrder.getCustomerId() != null){
			crmCustomerInstallInfo.updateById(customerInfo);
		} else {
			crmCustomerInstallInfo.insert(customerInfo);
		}
		if(installationOrderList!=null && installationOrderList.size()>0) {
			for(CrmInstallationOrder entity:installationOrderList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				installationOrderMapper.insert(entity);
			}
		}
		if(maintainOrdersList!=null && maintainOrdersList.size()>0) {
			for(CrmMaintainOrders entity:maintainOrdersList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				maintainOrdersMapper.insert(entity);
			}
		}
		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
			for(CrmWorkOrderProcess entity:workOrderProcessList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderProcessMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CrmWorkOrder workOrder,List<CrmInstallationOrder> installationOrderList,List<CrmMaintainOrders> maintainOrdersList,List<CrmWorkOrderProcess> workOrderProcessList,List<CrmAttendanceRecord> attendanceRecordList) {
		workOrderMapper.updateById(workOrder);
		
		//1.先删除子表数据
		installationOrderMapper.deleteByMainId(workOrder.getId());
		maintainOrdersMapper.deleteByMainId(workOrder.getId());
//		workOrderProcessMapper.deleteByMainId(workOrder.getId());
//		attendanceRecordMapper.deleteByMainId(workOrder.getId());
		
		//2.子表数据重新插入
		if(installationOrderList!=null && installationOrderList.size()>0) {
			for(CrmInstallationOrder entity:installationOrderList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				installationOrderMapper.insert(entity);
			}
		}
		if(maintainOrdersList!=null && maintainOrdersList.size()>0) {
			for(CrmMaintainOrders entity:maintainOrdersList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				maintainOrdersMapper.insert(entity);
			}
		}
//		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
//			for(WorkOrderProcess entity:workOrderProcessList) {
//				//外键设置
//				entity.setWorkOrderId(workOrder.getId());
//				workOrderProcessMapper.insert(entity);
//			}
//		}
//		if(attendanceRecordList!=null && attendanceRecordList.size()>0) {
//			for(AttendanceRecord entity:attendanceRecordList) {
//				//外键设置
//				entity.setWorkOrderId(workOrder.getId());
//				attendanceRecordMapper.insert(entity);
//			}
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		installationOrderMapper.deleteByMainId(id);
		maintainOrdersMapper.deleteByMainId(id);
		workOrderProcessMapper.deleteByMainId(id);
		workOrderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			installationOrderMapper.deleteByMainId(id.toString());
			maintainOrdersMapper.deleteByMainId(id.toString());
			workOrderProcessMapper.deleteByMainId(id.toString());
			workOrderMapper.deleteById(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CrmWorkOrder workOrder,List<CrmWorkOrderProcess> workOrderProcessList) {
		workOrderMapper.updateById(workOrder);

		//1.先删除子表数据
		/*installationOrderMapper.deleteByMainId(workOrder.getId());
		maintainOrdersMapper.deleteByMainId(workOrder.getId());
		workOrderProcessMapper.deleteByMainId(workOrder.getId());*/


		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
			for(CrmWorkOrderProcess entity:workOrderProcessList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderProcessMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateServiceRecords(CrmWorkOrder workOrder,List<CrmWorkServiceRecords> workServiceRecordsList,List<CrmWorkOrderProcess> workOrderProcessList) {
//		workOrderMapper.updateById(workOrder);

		//1.先删除子表数据
//		workServiceRecordsMapper.deleteByMainId(workOrder.getId());

		//2.子表数据重新插入
		if(workServiceRecordsList!=null && workServiceRecordsList.size()>0) {
			for(CrmWorkServiceRecords entity:workServiceRecordsList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workServiceRecordsMapper.insert(entity);
			}
		}
		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
			for(CrmWorkOrderProcess entity:workOrderProcessList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderProcessMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CrmWorkOrder workOrder,List<CrmWorkOrderProcess> workOrderProcessList,List<CrmAttendanceRecord> attendanceRecordList){
		workOrderMapper.updateById(workOrder);

		//1.先删除子表数据
//		workOrderProcessMapper.deleteByMainId(workOrder.getId());
//		attendanceRecordMapper.deleteByMainId(workOrder.getId());

		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
			for(CrmWorkOrderProcess entity:workOrderProcessList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderProcessMapper.insert(entity);
			}
		}
		if(attendanceRecordList!=null && attendanceRecordList.size()>0) {
			for(CrmAttendanceRecord entity:attendanceRecordList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				attendanceRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateFollowUp(CrmWorkOrder workOrder,List<CrmFollowUp> followUpList, List<CrmWorkOrderProcess> workOrderProcessList) {
		workOrderMapper.updateById(workOrder);

		//1.先删除子表数据
//		workServiceRecordsMapper.deleteByMainId(workOrder.getId());
		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
			for(CrmWorkOrderProcess entity:workOrderProcessList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderProcessMapper.insert(entity);
			}
		}
		//2.子表数据重新插入
		if(followUpList!=null && followUpList.size()>0) {
			for(CrmFollowUp entity:followUpList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				followUpMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateWorkOrderReceipt(CrmWorkOrder workOrder,List<CrmWorkOrderReceipt> workOrderReceiptList, List<CrmWorkOrderProcess> workOrderProcessList) {
		workOrderMapper.updateById(workOrder);

		//1.先删除子表数据
//		workServiceRecordsMapper.deleteByMainId(workOrder.getId());
		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
			for(CrmWorkOrderProcess entity:workOrderProcessList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderProcessMapper.insert(entity);
			}
		}
		//2.子表数据重新插入
		if(workOrderReceiptList!=null && workOrderReceiptList.size()>0) {
			for(CrmWorkOrderReceipt entity:workOrderReceiptList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				workOrderReceiptMapper.insert(entity);
			}
		}
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void receiptReview(CrmWorkOrder workOrder, List<CrmWorkOrderReceipt> workOrderReceiptList, List<CrmWorkOrderProcess> workOrderProcessList) {
		workOrderMapper.updateById(workOrder);

		//1.先删除子表数据
//		workServiceRecordsMapper.deleteByMainId(workOrder.getId());
//		if(workOrderProcessList!=null && workOrderProcessList.size()>0) {
//			for(WorkOrderProcess entity:workOrderProcessList) {
//				//外键设置
//				entity.setWorkOrderId(workOrder.getId());
//				workOrderProcessMapper.insert(entity);
//			}
//		}
		//获取登录用户信息
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		//2.子表数据重新插入
		if(workOrderReceiptList!=null && workOrderReceiptList.size()>0) {
			for(CrmWorkOrderReceipt entity:workOrderReceiptList) {
				//外键设置
				entity.setWorkOrderId(workOrder.getId());
				entity.setReviewer(sysUser.getRealname());
				entity.setReviewTime(new Date());
				workOrderReceiptMapper.updateById(entity);
//				workOrderReceiptMapper.insert(entity);
			}
		}
	}
	@Override
	@Transactional(rollbackFor = Exception.class)
	public CrmWorkOrderStatistics statistics(){
		return workOrderMapper.statistics();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public CrmWorkOrderStatistics statistics(String userName){
		return workOrderMapper.statisticsByUserName(userName);
	}
}
